package com.application.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class NetUtil {

    public static String doGet() {
        String result = "";
        BufferedReader reader = null;
        String bookJSONString = null;
        HttpURLConnection httpURLConnection = null;

        try {

            String url = "https://jsonplaceholder.typicode.com/posts/1";
            URL requestUrl = new URL(url);
            httpURLConnection = (HttpURLConnection) requestUrl.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.connect();

            InputStream inputStream = httpURLConnection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            StringBuilder builder = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append("\n");
            }
            if (builder.length() == 0) {
                return null;
            }
            result = builder.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (httpURLConnection!=null){
                httpURLConnection.disconnect();
            }
            if (reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return result;
    }

    public static boolean doPost(String urlStr) {
        HttpURLConnection urlConnection = null;
        OutputStream outputStream = null;
        boolean result = false;

        try {
            URL url = new URL(urlStr);
            urlConnection = (HttpURLConnection) url.openConnection();
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("userName", "lxd");
            paramMap.put("pass", "123");
            String paramDate = paramMapToString(paramMap);

            urlConnection.setRequestMethod("POST");
            urlConnection.setConnectTimeout(10000);
            urlConnection.setRequestProperty("Content-Length", String.valueOf(paramMap.size()));

            urlConnection.setDoInput(true);

            outputStream = urlConnection.getOutputStream();
            outputStream.write(paramDate.getBytes());

            int code = urlConnection.getResponseCode();
            if (code == 200) {
                result = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static String paramMapToString(Map<String, String> paraMap) {
        StringBuilder stringBuilder = new StringBuilder();
        Set<Map.Entry<String, String>> entries = paraMap.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            stringBuilder.append(entry.getKey())
                    .append("=")
                    .append(entry.getValue())
                    .append("&");
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        return stringBuilder.toString();
    }

}
